//
//  QKAlertManager.m
//  QKiOS
//
//  Created by lushitong on 2018/12/14.
//  Copyright © 2018年 qingka. All rights reserved.
//

#import "QKAlertManager.h"

@implementation QKAlertManager

/**
 * 自定义JKAlertView支持的任意样式弹框
 */
+ (void)showWithSuperView:(UIView *)superView
					title:(NSString *)title
				  message:(NSString *)message
					style:(JKAlertStyle)style
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	JKAlertView *alertView = JKAlertView.alertView(title, message, style).setCustomSuperView(superView);
	
	!config ? : config(alertView);
	
	alertView.show();
}

/**
 * 自定义JKAlertView支持的任意样式弹框
 */
+ (void)showWithSuperView:(UIView *)superView
		  attributedTitle:(NSAttributedString *)attributedTitle
		attributedMessage:(NSAttributedString *)attributedMessage
					style:(JKAlertStyle)style
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	JKAlertView *alertView = JKAlertView.alertViewAttributed(attributedTitle, attributedMessage, style).setCustomSuperView(superView);
	
	!config ? : config(alertView);
	
	alertView.show();
}

/**
 * 显示一个普通的无取消按钮的弹框
 * superView : 自定义superView，传nil则添加到keywindow
 * verifyTitle : 确认的文字，传nil表示没有下方按钮
 */
+ (void)showWithSuperView:(UIView *)superView
					title:(NSString *)title
				  message:(NSString *)message
			  verifyTitle:(NSString *)verifyTitle
			 verifyAction:(void(^)(void))verifyAction{
	
	[self showWithSuperView:superView title:title message:message cancelTitle:nil verifyTitle:verifyTitle verifyAction:^(JKAlertAction *action) {
		
		!verifyAction ? : verifyAction();
		
	} configBeforeShow:^(JKAlertView *alertView) {
		
		alertView.setCustomSuperView(superView);
	}];
}

/**
 * 自定义弹框，添加输入框等
 * 注意这里点击确定不会自动消失，需要手动消失 action.alertView.dismiss();
 * verifyTitle : 确定的文字，传nil表示没有下方按钮
 * closeAction : 点击叉号的操作
 * configBeforeShow : 用来自定义，比如添加输入框
 */
+ (void)showCustomWithTitle:(NSString *)title
				verifyTitle:(NSString *)verifyTitle
				closeAction:(void(^)(void))closeAction
			   verifyAction:(void(^)(JKAlertAction *action))verifyAction
		   configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	[self showWithSuperView:nil title:title message:nil cancelTitle:nil verifyTitle:verifyTitle isAutoDimiss:NO closeAction:closeAction cancelAction:nil verifyAction:verifyAction configBeforeShow:config];
}

/**
 * 显示密码框
 * 注意这里点击确定不会自动消失，需要手动消失 action.alertView.dismiss();
 */
+ (void)showPasswordAlertWithVerifyTitle:(NSString *)verifyTitle
						 textFieldConfig:(void(^)(UITextField *textField))textFieldConfig
							 closeAction:(void(^)(void))closeAction
							verifyAction:(void(^)(JKAlertAction *action, NSString *password))verifyAction{
	
	[self showPasswordWithSuperView:nil verifyTitle:verifyTitle textFieldConfig:textFieldConfig closeAction:closeAction verifyAction:verifyAction configBeforeShow:nil];
}


/**
 * 显示密码框
 * superView : 自定义superView，传nil则添加到keywindow
 * 注意这里点击确认不会自动消失，需要手动消失 action.alertView.dismiss();
 * configBeforeShow : 在show之前对一些特殊弹框做些配置，不需要直接传nil
 */
+ (void)showPasswordWithSuperView:(UIView *)superView
					  verifyTitle:(NSString *)verifyTitle
				  textFieldConfig:(void(^)(UITextField *textField))textFieldConfig
					  closeAction:(void(^)(void))closeAction
					 verifyAction:(void(^)(JKAlertAction *action, NSString *password))verifyAction
				 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	__block UITextField *outTextField = nil;
	
	[QKAlertManager showCustomWithTitle:verifyTitle verifyTitle:@"确定" closeAction:closeAction verifyAction:^(JKAlertAction *action) {
		
		!verifyAction ? : verifyAction(action, outTextField.text);
		
	} configBeforeShow:^(JKAlertView *alertView) {
		
		alertView.setCustomSuperView(superView);
		
		alertView.setTextViewLeftRightMargin(28);
		
		alertView.setCustomPlainTitleView(YES, ^UIView *(JKAlertView *view) {
			
			return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 27)];
		});
		
		alertView.addTextFieldWithConfigurationHandler(^(JKAlertView *view, UITextField *textField) {
			
			outTextField = textField;
			
			textField.placeholder = @"密码";
			textField.keyboardType = UIKeyboardTypeASCIICapable;
			textField.clearButtonMode = UITextFieldViewModeWhileEditing;
			
			textField.textAlignment = NSTextAlignmentCenter;
			textField.font = [UIFont systemFontOfSize:16];
			textField.textColor = [UIColor colorWithHexString:@"#000000"];
			textField.borderStyle = UITextBorderStyleNone;
			textField.layer.cornerRadius = 6;
			textField.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
			
			textField.superview.backgroundColor = nil;
			
			textField.frame = CGRectMake(0, 0, 0, 44);
			
			!textFieldConfig ? : textFieldConfig(textField);
		});
		
		alertView.addTextFieldWithConfigurationHandler(^(JKAlertView *view, UITextField *textField) {
			
			textField.hidden = YES;
			
			textField.frame = CGRectMake(0, 0, 0, 10);
		});
		
		alertView.setPlainCloseButtonConfig(^(UIButton *closeButton) {
			
			[closeButton JKAlertX_addClickOperation:^(id control) {
				
				!closeAction ? : closeAction();
			}];
		});
		
		alertView.setFullScreenBackGroundView(^UIView *{
			
			return nil;
		});
		
		!config ? : config(alertView);
	}];
}

/**
 * 显示普通文字弹框，默认行间距为5
 * cancelTitle : 取消的文字，传nil代表没有取消按钮
 * verifyTitle : 确定的文字，和cancelTitle同时传nil表示没有下方按钮
 * configBeforeShow : 在show之前对一些特殊弹框做些配置，不需要直接传nil
 */
+ (void)showWithSuperView:(UIView *)superView
					title:(NSString *)title
				  message:(NSString *)message
			  cancelTitle:(NSString *)cancelTitle
			  verifyTitle:(NSString *)verifyTitle
			 verifyAction:(void(^)(JKAlertAction *action))verifyAction
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	[self showWithSuperView:superView title:title message:message cancelTitle:cancelTitle verifyTitle:verifyTitle isAutoDimiss:YES closeAction:nil cancelAction:nil verifyAction:verifyAction configBeforeShow:config];
}

/**
 * 显示普通文字弹框，默认行间距为5
 * superView   : 自定义superView，传nil则添加到keywindow
 * cancelTitle : 取消的文字，传nil代表没有取消按钮
 * verifyTitle : 确定的文字，和cancelTitle同时传nil表示没有下方按钮
 * isAutoDimiss : 点击确定是否自动消失
 * configBeforeShow : 在show之前对一些特殊弹框做些配置，不需要直接传nil
 */
+ (void)showWithSuperView:(UIView *)superView
					title:(NSString *)title
				  message:(NSString *)message
			  cancelTitle:(NSString *)cancelTitle
			  verifyTitle:(NSString *)verifyTitle
			 isAutoDimiss:(BOOL)isAutoDimiss
			  closeAction:(void(^)(void))closeAction
			 cancelAction:(void(^)(JKAlertAction *action))cancelAction
			 verifyAction:(void(^)(JKAlertAction *action))verifyAction
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	NSAttributedString *titleAttr = nil;
	NSAttributedString *messageAttr = nil;
	
	NSMutableParagraphStyle *titlePara = [[NSMutableParagraphStyle alloc] init];
	titlePara.alignment = NSTextAlignmentCenter;
	
	if (title) {
		
		titleAttr = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:18], NSParagraphStyleAttributeName : titlePara}];
	}
	
	if (message) {
		
		NSMutableParagraphStyle *messagePara = [titlePara mutableCopy];
		messagePara.lineSpacing = 5;
		
		messageAttr = [[NSAttributedString alloc] initWithString:message attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#333333"], NSFontAttributeName : [UIFont systemFontOfSize:16], NSParagraphStyleAttributeName : messagePara}];
	}
	
	[self showWithSuperView:superView attributedTitle:titleAttr attributedMessage:messageAttr cancelTitle:cancelTitle verifyTitle:verifyTitle isAutoDimiss:isAutoDimiss closeAction:closeAction cancelAction:cancelAction verifyAction:verifyAction configBeforeShow:config];
}

/**
 * 显示富文本弹框
 * superView   : 自定义superView，传nil则添加到keywindow
 * cancelTitle : 取消的文字，传nil代表没有取消按钮
 * verifyTitle : 确认的文字，和cancelTitle同时传nil表示没有下方按钮
 * configBeforeShow : 在show之前对一些特殊弹框做些配置，不需要直接传nil
 */
+ (void)showWithSuperView:(UIView *)superView
		  attributedTitle:(NSAttributedString *)attributedTitle
		attributedMessage:(NSAttributedString *)attributedMessage
			  cancelTitle:(NSString *)cancelTitle
			  verifyTitle:(NSString *)verifyTitle
			 verifyAction:(void(^)(JKAlertAction *action))verifyAction
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	[self showWithSuperView:superView attributedTitle:attributedTitle attributedMessage:attributedMessage cancelTitle:cancelTitle verifyTitle:verifyTitle isAutoDimiss:YES closeAction:nil cancelAction:nil verifyAction:verifyAction configBeforeShow:config];
}

/**
 * 显示富文本弹框
 * superView    : 自定义superView，传nil则添加到keywindow
 * cancelTitle  : 取消的文字，传nil代表没有取消按钮
 * verifyTitle  : 确定的文字，和cancelTitle同时传nil表示没有下方按钮
 * isAutoDimiss : 点击确定否自动消失
 * configBeforeShow : 在show之前对一些特殊弹框做些配置，不需要直接传nil
 */
+ (void)showWithSuperView:(UIView *)superView
		  attributedTitle:(NSAttributedString *)attributedTitle
		attributedMessage:(NSAttributedString *)attributedMessage
			  cancelTitle:(NSString *)cancelTitle
			  verifyTitle:(NSString *)verifyTitle
			 isAutoDimiss:(BOOL)isAutoDimiss
			  closeAction:(void(^)(void))closeAction
			 cancelAction:(void(^)(JKAlertAction *action))cancelAction
			 verifyAction:(void(^)(JKAlertAction *action))verifyAction
		 configBeforeShow:(void(^)(JKAlertView *alertView))config{
	
	JKAlertView *alertView = JKAlertView.alertViewAttributed(attributedTitle, attributedMessage, JKAlertStylePlain);
	
	alertView.setCustomSuperView(superView);
	
	// 圆角
	alertView.setPlainCornerRadius(15);
	
	// title字体和颜色
	alertView.setTitleTextFont([UIFont systemFontOfSize:18]).setTitleTextColor([UIColor blackColor]);
	
	// title和message分隔线    title上下间距
	alertView.setPlainTitleMessageSeparatorHidden(NO, 0).setTextViewTopBottomMargin(attributedTitle ? 15 : 30);
	
	UIFont *messageFont = [UIFont systemFontOfSize:16];
	
	// message字体和颜色
	alertView.setMessageTextFont(messageFont).setMessageTextColor([UIColor colorWithHexString:@"#000000"]);
	
	// message最小高度及上下间距
	alertView.setMessageMinHeight(60).setTitleMessageMargin(15);
	
	// title message左右间距
	alertView.setTextViewLeftRightMargin(20);
	
	if (cancelTitle) { // 有取消
		
		alertView.addAction(JKAlertAction.action(cancelTitle, JKAlertActionStyleDefault, ^(JKAlertAction *action) {
			
			!cancelAction ? : cancelAction(action);
			
		}).setAutoDismiss(isAutoDimiss).setTitleColor([UIColor colorWithHexString:@"#000000"]).setTitleFont([UIFont systemFontOfSize:14])).addAction(JKAlertAction.action(verifyTitle, JKAlertActionStyleDefault, ^(JKAlertAction *action) {
			
			!verifyAction ? : verifyAction(action);
			
		}).setAutoDismiss(isAutoDimiss).setTitleColor([UIColor colorWithHexString:@"#000000"]).setTitleFont([UIFont systemFontOfSize:14]));
		
	}else{ // 无取消
		
		if (verifyTitle) { // 有确定
			
			// 添加自定义action
			[self addVerifyActionWithAlertView:alertView verifyTitle:verifyTitle isAutoDimiss:isAutoDimiss isMiddleButton:NO verifyAction:verifyAction];
			
		}else{ // 无确定
			
			alertView.addAction(JKAlertAction.action(nil, JKAlertActionStyleDefault, nil).setSeparatorLineHidden(YES).setCustomView(^UIView *(JKAlertAction *action) {
				
				return [[UIView alloc] init];
			}));
		}
	}
	
	// 通用配置
	[self setAllCommonConfig:alertView];
	
	if (!attributedTitle && attributedMessage) {
		
		// message最小高度及上下间距
		alertView.setMessageMinHeight(0).setTitleMessageMargin(30);
		
		alertView.setPlainCloseButtonConfig(^(UIButton *closeButton) {
			
			closeButton.hidden = YES;
		});
	}
	
	alertView.setPlainCloseButtonConfig(^(UIButton *closeButton) {
		
		[closeButton JKAlertX_addClickOperation:^(id control) {
			
			!closeAction ? : closeAction();
		}];
	});
	
	!config ? : config(alertView);
	
	alertView.show();
}
@end
